#include "LoginWidget.h"

#include <QVBoxLayout>
#include <QDebug>
#include <QMessageBox>

LoginWidget::LoginWidget(QWidget *parent):
        QWidget(parent)
{

    loginWindow= new LoginWindow(this);
    loginWindow->setVisible(false);

    login = new QPushButton(this);
    login->setText("Login");

    logout = new QPushButton(this);
    logout->setText("Log out");
    logout->setVisible(false);

    enter = new QPushButton(this);
    enter->setText("Log in");
    enter->setVisible(false);

    cancel = new QPushButton(this);
    cancel->setText("Cancel");
    cancel->setVisible(false);



    name = new QLabel(this);
    name->setText("Dietsystem");

    QHBoxLayout* l1 = new QHBoxLayout();
    l1->addWidget(name);
    l1->addWidget(login);
    l1->addWidget(logout);


    QHBoxLayout* l2 = new QHBoxLayout();
    l2->addWidget(enter);
    l2->addWidget(cancel);

    //l1->addWidget(loginWindow);
    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->addWidget(loginWindow);
    mainLayout->addLayout(l1);
     mainLayout->addLayout(l2);

    setLayout(mainLayout);
    connect(login, SIGNAL(clicked()),
            this, SLOT(onLoginButton()));
    connect(logout, SIGNAL(clicked()),
            this, SLOT(onLogoutButton()));
    connect(cancel, SIGNAL(clicked()),
            this, SLOT(onCancel()));
    connect(enter, SIGNAL(clicked()),
            this, SLOT(onEnterButton()));


    connect(&query,SIGNAL(responseReceived(QString, QString, QString)),
            this,SLOT(responseReceived(QString,QString)));
    connect(&query,SIGNAL(errorReceived()),this,SLOT(errorReceived()));
    connect(this, SIGNAL(onSucsess(QString)),this, SLOT(inDietSystem()));

    connect(this, SIGNAL(onSucsess(QString)),this, SLOT(getWeight(QString)));
    connect(&w_query,SIGNAL(responseReceived(QString,QDate,double)),
              this,SLOT(weightResponseReceived(QString,QDate,double)));
    connect(&w_query,SIGNAL(errorWReceived()),this,SLOT(errorReceived()));


}

void LoginWidget::onLoginButton()
{
    name->setVisible(false);
    login->setVisible(false);
    loginWindow->setVisible(true);
    enter->setVisible(true);
    cancel->setVisible(true);
}

void LoginWidget::onLogoutButton()
{
    name->setText("Dietsystem");
    login->setVisible(true);
    logout->setVisible(false);
    login->setDisabled(false);
    name->setVisible(true);
    emit logoutSignal();
}

void LoginWidget::onCancel()
{
    name->setVisible(true);
    login->setVisible(true);
    loginWindow->setVisible(false);
    enter->setVisible(false);
    cancel->setVisible(false);
    login->setDisabled(false);
}

void LoginWidget::onEnterButton()
{
    loginWindow->setLogin();
    loginWindow->setPassword();
    qDebug() << "query";
    query.setQuery(loginWindow->getLogin(), loginWindow->getPassword());
    query.doRequest();
    loginWindow->setVisible(false);
    enter->setVisible(false);
    cancel->setVisible(false);
    name->setVisible(true);
    login->setVisible(true);
    login->setDisabled(true);


}

void LoginWidget::responseReceived(QString status, QString auth_token)
{
    qDebug() << "received login query response";
    if(status=="Ok")
    {
        emit onSucsess(auth_token);

    }
    else
    {
        QMessageBox::information( this, "DietSystem","Wrong user name or password.");
        onLogoutButton();
        //error
    }
}


void LoginWidget::weightResponseReceived(QString status,QDate date, double weight)
{
    qDebug() << "received weight query response";
    if(status=="Ok")
    {
        emit onGetWeightSucsess(date, weight);
        qDebug()<<"In emit:"<<date;

    }
    else
    {
        QMessageBox::information( this, "DietSystem","Don't correct token");

        //error
    }
}


void LoginWidget::getWeight(QString auth_token)
{

    w_query.setQuery(auth_token);
    w_query.doRequest();
}


void LoginWidget::errorReceived()
{
     QMessageBox::information(this, "DietSystem", "Network error");
     onLogoutButton();
}



void LoginWidget::inDietSystem()
{
    login->setVisible(false);
    logout->setVisible(true);
    name->setText(loginWindow->getLogin());
}

void LoginWidget::addWeight(QString auth_token, double weight)
{
    w_query.setQueryToAdd(auth_token, weight);

    w_query.doRequestToAdd();
}

